/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejercicio.practica10;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author manolo
 */
class Triatleta implements Runnable {

    private CyclicBarrier barrera = null;
    private int numCorredores;
    private static double[] Parcial;
    private static double[] Total;
    private int dorsal;

    public Triatleta(int numCorredores) {
        Parcial = new double[numCorredores];
        Total = new double[numCorredores];
        this.numCorredores = numCorredores;
    }

    public Triatleta(CyclicBarrier barrera, int dorsal) {
        this.barrera = barrera;
        this.dorsal = dorsal;
    }

    public void ganador() {
        double mejorTiempo = Total[0];
        int ganador = 0;
        for (int i = 0; i < numCorredores; i++) {
            if (Total[i] < mejorTiempo) {
                mejorTiempo = Total[i];
                ganador = i;
            }
        }
        System.out.println("El ganador ha sido el dorsal " + (ganador + 1) + " con un tiempo de " +TimeUnit.MILLISECONDS.convert((long) mejorTiempo, TimeUnit.NANOSECONDS) + " mseg.");
    }

    public void run() {
        try {
            int i = barrera.await();
        } catch (InterruptedException | BrokenBarrierException ex) {
            System.out.println("Error: " + ex);
        }
        double tIni = System.nanoTime();
        try {
            Parcial[dorsal] = 0;
            Thread.sleep((int) (100 * Math.random()));
        } catch (InterruptedException ex) {
             System.out.println("Error: " + ex);
        }
        double t = System.nanoTime() - tIni;
        Parcial[dorsal] += t;
        Total[dorsal] += t;
    }
}

public class triatlonBarreras {

    public static void main(String[] args) throws InterruptedException {
        int numCorredores = 100;
        CyclicBarrier Meta = new CyclicBarrier(numCorredores);

        Triatleta c = new Triatleta(numCorredores);

        ExecutorService e = Executors.newFixedThreadPool(numCorredores);

        for (int i = 0; i < numCorredores; i++) {
            e.submit(new Triatleta(Meta, i));
        }
        System.out.println("Se ha completado la prueba de natación.");
        try {
            int i = Meta.await();
        } catch (InterruptedException | BrokenBarrierException ex) {
             System.out.println("Error: " + ex);
        }

        Meta.reset();
        for (int i = 0; i < numCorredores; i++) {
            e.submit(new Triatleta(Meta, i));
        }
        System.out.println("Se ha completado la prueba de ciclismo.");
        try {
            int i = Meta.await();
        } catch (InterruptedException | BrokenBarrierException ex) {
             System.out.println("Error: " + ex);
        }
        Meta.reset();
        for (int i = 0; i < numCorredores; i++) {
            e.submit(new Triatleta(Meta, i));
        }
        System.out.println("Se ha completado la prueba de fondo.");
        try {
            int i = Meta.await();
        } catch (InterruptedException | BrokenBarrierException ex) {
             System.out.println("Error: " + ex);
        }
        c.ganador();
        System.exit(0);
    }

}
